//
//  ViewController.swift
//  LImagePickerDemo
//
//  Created by less on 2019/10/9.
//  Copyright © 2019 Less. All rights reserved.
//

import UIKit
import LImagePicker
import Kingfisher

class ViewController: UIViewController {

    fileprivate var animationDelegate: ModelAnimationDelegate?

    fileprivate var dataArray = [LMediaResourcesModel]()
    
    fileprivate lazy var collectionView: UICollectionView = {
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.minimumLineSpacing = 1
        flowLayout.minimumInteritemSpacing = 1
        flowLayout.itemSize = CGSize(width: (UIScreen.main.bounds.width - 3)/4, height: (UIScreen.main.bounds.width - 3)/4)
        
        let collection = UICollectionView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height), collectionViewLayout: flowLayout)
        collection.delegate = self
        collection.dataSource = self
        collection.backgroundColor = UIColor.white
        return collection
    }()
    
    let resourcesArr: Array = ["https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1569607247442&di=1c44c45875a454c80e0e83e4b696bce5&imgtype=0&src=http%3A%2F%2Fimage.yy.com%2Fyywebalbumbs2bucket%2F3b30b885206740268a240296fc55ac12_1439484916392.jpg","https://static.chuanghehui.com/xcxmatrix/chuangjian/0901musk.mp4","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1569607247442&di=1c44c45875a454c80e0e83e4b696bce5&imgtype=0&src=http%3A%2F%2Fimage.yy.com%2Fyywebalbumbs2bucket%2F3b30b885206740268a240296fc55ac12_1439484916392.jpg","https://static.chuanghehui.com/xcxmatrix/chuangjian/0829%E5%B7%B4%E8%8F%B2%E7%89%B9%26%E6%AF%94%E7%9B%96%E8%8C%A8.mp4","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1569607247442&di=1c44c45875a454c80e0e83e4b696bce5&imgtype=0&src=http%3A%2F%2Fimage.yy.com%2Fyywebalbumbs2bucket%2F3b30b885206740268a240296fc55ac12_1439484916392.jpg","https://static.chuanghehui.com/xcxmatrix/chuangjian/0822BSN%E6%AF%95%E4%B8%9A%E7%AD%94%E8%BE%A9.mp4","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1569607247442&di=1c44c45875a454c80e0e83e4b696bce5&imgtype=0&src=http%3A%2F%2Fimage.yy.com%2Fyywebalbumbs2bucket%2F3b30b885206740268a240296fc55ac12_1439484916392.jpg","https://static.chuanghehui.com/xcxmatrix/chuangjian/0820mask.mp4","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1569607247442&di=1c44c45875a454c80e0e83e4b696bce5&imgtype=0&src=http%3A%2F%2Fimage.yy.com%2Fyywebalbumbs2bucket%2F3b30b885206740268a240296fc55ac12_1439484916392.jpg","https://static.chuanghehui.com/xcxmatrix/chuangjian/0816%E7%8E%8B%E5%BB%B6%E5%B3%B0vlog1.mp4","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1569607247442&di=1c44c45875a454c80e0e83e4b696bce5&imgtype=0&src=http%3A%2F%2Fimage.yy.com%2Fyywebalbumbs2bucket%2F3b30b885206740268a240296fc55ac12_1439484916392.jpg","https://static.chuanghehui.com/xcxmatrix/chuangjian/0816%E8%BF%88%E5%85%8B%E7%9B%96%E6%A0%BCvlog1.mp4","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1569607247442&di=1c44c45875a454c80e0e83e4b696bce5&imgtype=0&src=http%3A%2F%2Fimage.yy.com%2Fyywebalbumbs2bucket%2F3b30b885206740268a240296fc55ac12_1439484916392.jpg","https://static.chuanghehui.com/xcxmatrix/chuangjian/0816%E8%BF%88%E5%85%8B%E7%9B%96%E6%A0%BCvlog2.mp4","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1569607247442&di=1c44c45875a454c80e0e83e4b696bce5&imgtype=0&src=http%3A%2F%2Fimage.yy.com%2Fyywebalbumbs2bucket%2F3b30b885206740268a240296fc55ac12_1439484916392.jpg","https://static.chuanghehui.com/xcxmatrix/chuangjian/jobs.mp4","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1569607247442&di=1c44c45875a454c80e0e83e4b696bce5&imgtype=0&src=http%3A%2F%2Fimage.yy.com%2Fyywebalbumbs2bucket%2F3b30b885206740268a240296fc55ac12_1439484916392.jpg","https://static.chuanghehui.com/xcxmatrix/chuangjian/%E5%93%88%E7%BB%A7%E9%93%AD0809.mp4","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1569607247442&di=1c44c45875a454c80e0e83e4b696bce5&imgtype=0&src=http%3A%2F%2Fimage.yy.com%2Fyywebalbumbs2bucket%2F3b30b885206740268a240296fc55ac12_1439484916392.jpg","https://static.chuanghehui.com/xcxmatrix/chuangjian/0816%E8%B7%AF%E6%BC%94%E8%8A%B1%E7%B5%AE%20vlog.mp4","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1569607247442&di=1c44c45875a454c80e0e83e4b696bce5&imgtype=0&src=http%3A%2F%2Fimage.yy.com%2Fyywebalbumbs2bucket%2F3b30b885206740268a240296fc55ac12_1439484916392.jpg","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1569607247442&di=1c44c45875a454c80e0e83e4b696bce5&imgtype=0&src=http%3A%2F%2Fimage.yy.com%2Fyywebalbumbs2bucket%2F3b30b885206740268a240296fc55ac12_1439484916392.jpg"]

    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        initData()
        title = "首页"
        view.backgroundColor = UIColor.white
        collectionView.register(CollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
        view.addSubview(collectionView)
        
    }

    
    fileprivate func initData() {
        
        for (i, item) in resourcesArr.enumerated() {
            if i%2 == 0 {
                let model = LMediaResourcesModel(dataProtocol: item, dateEnum: .image, message: "sdsadsdasdasd")
                dataArray.append(model)
            }else {
                let model = LMediaResourcesModel(dataProtocol: item, dateEnum: .video, videoCover: "", message: "sdsadsdasdasd")
                dataArray.append(model)
            }
        }
    }

}

extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource,ShowImageProtocol, UIViewControllerTransitioningDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return resourcesArr.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: CollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
        if let imageStr = dataArray[indexPath.row].dataProtocol as? String {
            if imageStr.hasSuffix(".mp4") {
                if let url = URL(string: "https://static.chuanghehui.com/CJVlog/1567315072042.jpg") {
                    cell.contentImage.kf.setImage(with: ImageResource(downloadURL: url), placeholder: UIImage(named: "pic"))
                }
            }else {
                if let url = URL(string: imageStr) {
                    cell.contentImage.kf.setImage(with: ImageResource(downloadURL: url), placeholder: UIImage(named: "pic"))
                }
            }
        }else if let image  = dataArray[indexPath.row].dataProtocol as? UIImage {
            cell.contentImage.image = image
        }
        if indexPath.row == dataArray.count - 1 {
            cell.contentImage.image = nil
            cell.contentImage.backgroundColor = UIColor.red
        }
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print(indexPath)
        guard let cell = collectionView.cellForItem(at: indexPath) as? CollectionViewCell else { return }
        if indexPath.row == dataArray.count - 1 {
            
        }else {
            animationDelegate = ModelAnimationDelegate(contentImage: cell.contentImage, superView: collectionView)
            var array = dataArray
            array.removeLast()
            showImage(ShowImageConfiguration(dataArray: array, currentIndex: indexPath.item),delegate: animationDelegate, formVC: self)
            
        }
    }
    
}

extension ViewController: ShowImageVCDelegate {
    
    func showImageDidDelete(_ viewController: ShowImageViewController, index: Int, imageData: LMediaResourcesModel) {
    }
}
